package ca.kooki.codesnippet;

import java.util.ArrayList;
import java.util.List;

public class Description {

  //```````````````````````
  // MEMBER VARIABLES
  //```````````````````````
  
  private String _title;
  private String _keywords;
    
  //```````````````````````
  // CONSTRUCTOR
  //```````````````````````

  public Description(String title, String keywords)
  {
    _title = title;
    _keywords = keywords;
  }
 
  
  //```````````````````````
  // INTERFACE
  //```````````````````````
  
  public String getTitle() 
  {
    return _title;
  }

  public String getKeywords() 
  {
    return _keywords;
  }

  public String[] getKeywordList()
  {
    String[] rawList = _keywords.split(" ");
    List<String> trueList = new ArrayList<String>();
    
    for (int i=0; i< rawList.length; i++)
    {
      String potentialKeyword = rawList[i].trim();
      if (potentialKeyword.length() > 0)
      {
        trueList.add(potentialKeyword);
      }
    }
    
    return (String[])trueList.toArray(new String[trueList.size()]);
  }
  
  public boolean contains(String keywords)
  {
    if (keywords == null || keywords.equals(""))
    {
      return true;
    }
    
    String[] allSearchTerms = keywords.split(" ");
    String[] allKeywords = getKeywordList();
    
    for (int i=0; i<allSearchTerms.length; i++)
    {
      for (int j=0; j<allKeywords.length; j++)
      {
        if (allSearchTerms[i].equals(allKeywords[j]))
        {
          return true;
        }
      }
    }
    return false;
  }
  
  public String toString()
  {
    return String.format("%s;%s;", _title,_keywords);
  }
  
  public boolean equals(Object obj)
  {
    if (obj == null || !obj.getClass().equals(Description.class))
    {
      return false;
    }

    Description compareTo = (Description)obj;
    return this.toString().equals(compareTo.toString());
  }

  
}
